function getCookie() {
    const id = self.location.href.split('=')[1];
        console.log(id);
        const ID = $.cookie('ID');
        // const data = localStorage.getItem(ID + "&" + id);

        let arr = new Array();
        let res = new Array();
        for (let i = 0; i < localStorage.length; i++) {
            let key = localStorage.key(i);
            let value = localStorage.getItem(key);
            arr.push({
            key: key,
            value: value,
        })
    }
    console.log(arr);

    for (let i = 0; i < arr.length; i++) {
        // console.log(arr[i].key.split("&")[0]);
        if (arr[i].key.split("&")[0] != ID) continue;
        res.push(JSON.parse(localStorage.getItem(arr[i].key)));
    }
    console.log(res);

    // const res = JSON.parse(data);
    // console.log(res);
    return res;
}

// 全选
function selectAll() {
    let flg = true;
    $('#selectAll').on('click', () => {
        if (flg) {
            $('input').attr('checked', true);
            flg = false;
        }else {
            flg = true;
            $('input').attr('checked', false);
        }

    });
}


selectAll();

function remove(ele) {
    ele.on('click', function () {
        // console.log(this);
        const id = this.getAttribute('id');
        // console.log(id);
        const ID = $.cookie('ID');
        // console.log(ID)
        // 删除数据
        localStorage.removeItem(ID +'&'+id);
        // 删除节点
        // console.log($("#"+id));
        $("#" + id).parent().parent().parent().remove();
    })
}


function random() {
    let data = [];
    data = getCookie();
    // console.log(res);
    let res = data.map((item) => {
        return `
                <dd  class="dd5" id="dd5">
                    <p><span>满减</span> 每满5000减800元,上不封顶!</p>
                    <input type="checkbox">
                    <img id="limg" src="${item.src}" alt="">
                    <p class="p2">${item.title}<br>橡胶蓝色</p>
                    <div>
                        <p>${item.price}</p>
                        <table>
                            <tr>
                                <td><button>-</button></td>
                                <td id="inp"><input type="text" placeholder="1"></td>
                                <td><button>+</button></td>
                            </tr>
                        </table>
                        <p class="p-r">${item.price}</p>
                        <p class="p-rr"><button class="remove" id="${item.id}">删除</button><br>移入收藏</p>
                    </div>
                </dd>`
    }).join("");
    $('#dl').append(res);
    remove($('.remove'));
}

function removeDetail() {
    let arr = new Array();
    for (let i = 0; i < localStorage.length; i++) {
        let key = localStorage.key(i);
        let value = localStorage.getItem(key);
        arr.push({
            key: key,
            value: value,
        })
    }
    for (let i=0; i<localStorage.length; i++)
    {
        let key = localStorage.key(i);
        if (/^Detail/.test(key)){
            console.log(key);
            localStorage.removeItem(key);
        }
        // console.log(/^Detail/.test(key))
    }
    console.log('re:', arr);
}

removeDetail();

random();